import qs from 'qs';
import {
    mmodel
} from '../api';
let Slist = [],
    SMore;
let msglist = [],
    msgtotal = 1,
    msgmore = false;
export default {
    namespace: 'mmodel',
    state: {
        userMsg: {
            id: null,
            loginAccount: null,
            location: null
        },
        data: [],
        personallist: [],
        expressData: [],
        kData: {},
        SList: [],
        uAdressData: {},
        SMore: false,
        msglist: [],
        searchlist: [],
        msgtotal: 1,
        msgmore: false,
        systemData: {},
        savePointData: {},
        savePersonSeslect: {}
    },
    reducers: {
        queryListSuccess(state, {
            payload
        }) {
            return {
                ...state,
                ...payload
            };
        }
    },
    effects: {
        * msgserch({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.msgquery, params);
            // console.log(jsonResult.data)

            if (jsonResult.data != '[]') {
                if (jsonResult.data.page == 1) {
                    msglist = [];
                }
                // console.log(user_milk)
                msgtotal = jsonResult.data.total_page;
                if (jsonResult.data && jsonResult.data.list != null && jsonResult.data.list.length > 0) {
                    msgmore = true;
                    for (var i = 0; i < jsonResult.data.list.length; i++) {
                        msglist.push(jsonResult.data.list[i]);
                    }
                } else {
                    msgmore = false;
                }

            } else {
                msglist = [];
                msgmore = false;
            }

            if (typeof func === 'function') {
                func(jsonResult.data);
            }

            // console.log(msglist);
            yield put({
                type: 'queryListSuccess',
                payload: {
                    msgList: msglist,
                    msgtotal: msgtotal,
                    msgmore: msgmore
                }
            });
        },
        * Personalserch({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.Personalserch, params);
            let list = [];
            if (jsonResult.data && jsonResult.data.list && jsonResult.data.list.length > 0) {
                list = jsonResult.data.list;
            }
            yield put({
                type: 'queryListSuccess',
                payload: {
                    personallist: list
                }
            });
        },

        * hot_keywords({
            payload: params
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.hot_keywords, params);
            yield put({
                type: 'queryListSuccess',
                payload: {
                    searchlist: jsonResult.data
                }
            });
        },
        * indexdata({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.indexdata, params);
            // console.log(jsonResult.data)
            yield put({
                type: 'queryListSuccess',
                payload: {
                    systemData: jsonResult.data
                }
            });

            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },
        * serchpersonal({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.serchpersonal, params);
            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },
        * dataIndex({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.dataIndex, params);
            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },

        * saveStatus({
            payload: params
        }, {
            call,
            put
        }) {
            // console.log(params)
            yield put({
                type: 'queryListSuccess',
                payload: {
                    urData: params
                }
            });
        },
        * saveDetail({
            payload: params
        }, {
            call,
            put
        }) {
            // console.log(params)
            yield put({
                type: 'queryListSuccess',
                payload: {
                    uAdressData: params
                }
            });
        },
        * save({
            payload: params
        }, {
            call,
            put
        }) {
            yield put({
                type: 'queryListSuccess',
                payload: {
                    saveSeslect: params
                }
            });
        },
        * savePersonal({
            payload: params
        }, {
            call,
            put
        }) {
            yield put({
                type: 'queryListSuccess',
                payload: {
                    savePersonSeslect: params
                }
            });
        },

        * saveVideoStatus({
            payload: params
        }, {
            call,
            put
        }) {
            // console.log(params)
            yield put({
                type: 'queryListSuccess',
                payload: {
                    videoData: params
                }
            });
        },
        * queryExpressCode({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.queryExpressCode, params);
            yield put({
                type: 'serchSuccess',
                payload: {
                    expressData: jsonResult.data
                }
            });
            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },
        * queryExpress({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.queryExpress, params);

            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },
        * getExpressCom({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.getExpressCom, params);

            if (typeof func === 'function') {
                func(jsonResult);
            }
        },
        * qiniuToken({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.qiniuToken, params);

            if (typeof func === 'function') {
                func(jsonResult);
            }
        },

        * getExpressCom({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.getExpressCom, params);

            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },
        * getExpressRefuse({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.getExpressRefuse, params);

            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },
        * queryUpdate({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.update, params);

            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },
        * confirm({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.confirm, params);

            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },
        * getWechatConfig({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.getWechatConfig, params);

            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },
        * expressindex({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.expressindex, params);
            if (jsonResult.data != '[]') {
                if (params.page == 1) {
                    Slist = [];
                }
                // Stotal=jsonResult.data.total;
                // console.log(params)
                if (jsonResult.data && jsonResult.data.list != null && jsonResult.data.list.length > 0) {
                    SMore = true;
                    for (var i = 0; i < jsonResult.data.list.length; i++) {
                        Slist.push(jsonResult.data.list[i]);
                    }
                } else {
                    if (params.page == 1) {
                        Slist = [];
                        SMore = false;
                    }

                }

            } else {
                Slist = [];
                SMore = false;
                // Stotal:0;
            }
            // if (typeof func === 'function') {
            //     func(jsonResult.data);
            // }
            yield put({
                type: 'queryListSuccess',
                payload: {
                    Slist: Slist,
                    SMore: SMore
                }
            });
        },
        * savePoint({
            payload: params
        }, {
            call,
            put
        }) {
            yield put({
                type: 'queryListSuccess',
                payload: {
                    savePointData: params
                }
            });
        },
        * serch({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {

                jsonResult
            } = yield call(mmodel.query, params);
            // console.log(jsonResult.data)
            yield put({
                type: 'queryListSuccess',
                payload: {
                    list: jsonResult.data
                }
            });
            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },
        * comment({
            payload: {
                params,
                func
            }
        }, {
            call,
            put
        }) {
            const {
                jsonResult
            } = yield call(mmodel.comment, params);

            if (typeof func === 'function') {
                func(jsonResult.data);
            }
        },


    }
};